<template>
  <div>
    <div style="display: flex;justify-content: flex-start">
      <el-input
        placeholder="通过标题搜索该分类下的博客..."
        prefix-icon="el-icon-search"
        v-model="keywords"
        style="width: 400px"
        size="mini"
      >
      </el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="mini"
        style="margin-left: 3px"
        @click="searchClick"
        >搜索
      </el-button>
    </div>
    <!--<div style="width: 100%;height: 1px;background-color: #20a0ff;margin-top: 8px;margin-bottom: 0px"></div>-->
    <el-table
      ref="multipleTable"
      :data="articles"
      tooltip-effect="dark"
      style="width: 100%;overflow-x: hidden; overflow-y: hidden;"
      max-height="390"
      @selection-change="handleSelectionChange"
      v-loading="loading"
    >
      <el-table-column
        type="selection"
        width="35"
        align="left"
        v-if="showEdit || showDelete"
      >
      </el-table-column>
      <el-table-column label="标题" width="400" align="left">
        <template slot-scope="scope"
          ><span
            style="color: #409eff;cursor: pointer"
            @click="itemClick(scope.row)"
            >{{ scope.row.title }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="最近编辑时间" width="140" align="left">
        <template slot-scope="scope">{{ scope.row.updateTime }}</template>
      </el-table-column>
      <el-table-column
        prop="user.nickName"
        label="作者"
        width="120"
        align="left"
      >
      </el-table-column>
      <el-table-column
        prop="category.name"
        label="所属分类"
        width="120"
        align="left"
      >
      </el-table-column>
      <el-table-column label="操作" align="left" v-if="showEdit || showDelete">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            v-if="showEdit"
            >编辑
          </el-button>
          <el-button
            size="mini"
            @click="handleRestore(scope.$index, scope.row)"
            v-if="showRestore"
            >还原
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            v-if="showDelete"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="blog_table_footer">
      <el-button
        type="danger"
        size="mini"
        style="margin: 0px;"
        v-show="this.articles.length > 0 && showDelete"
        :disabled="this.selItems.length == 0"
        @click="deleteMany"
        >批量删除
      </el-button>
      <span></span>
      <el-pagination
        background
        :page-size="pageSize"
        layout="prev, pager, next"
        :total="totalCount"
        @current-change="currentChange"
        v-show="this.articles.length > 0"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { get, put, del } from "../utils/api";

export default {
  data() {
    return {
      articles: [],
      selItems: [],
      loading: false,
      currentPage: 1,
      totalCount: -1,
      pageSize: 6,
      keywords: "",
      dustbinData: []
    };
  },
  props: ["state", "showEdit", "showDelete", "activeName", "showRestore"],
  mounted: function() {
    this.loading = true;
    this.loadBlogs(1, this.pageSize);
  },

  methods: {
    searchClick() {
      this.loadBlogs(1, this.pageSize);
    },
    itemClick(row) {
      this.$router.push({ path: "/details", query: { aid: row.id } });
    },
    deleteMany() {
      var selItems = this.selItems;
      for (var i = 0; i < selItems.length; i++) {
        this.dustbinData.push(selItems[i].id);
      }
      this.deleteToDustBin(selItems[0].state);
    },
    //翻页
    currentChange(currentPage) {
      this.currentPage = currentPage;
      this.loading = true;
      this.loadBlogs(currentPage, this.pageSize);
    },
    loadBlogs(page = 1, count = 6) {
      var url = "";
      if (this.state == -2) {
        url =
          "/article/all" +
          "?page=" +
          page +
          "&count=" +
          count +
          "&keywords=" +
          this.keywords;
      } else {
        url =
          "/article/?status=" +
          this.state +
          "&page=" +
          page +
          "&count=" +
          count +
          "&keywords=" +
          this.keywords;
      }
      get(url).then(resp => {
        this.loading = false;
        if (resp.code == 0) {
          this.articles = resp.data.data;
          this.totalCount = resp.data.count;
        } else {
          this.$message({ type: "error", message: "数据加载失败!" });
        }
      });
    },
    handleSelectionChange(val) {
      this.selItems = val;
    },
    handleEdit(index, row) {
      this.$router.push({
        path: "/edit",
        query: { aid: row.id }
      });
    },
    handleDelete(index, row) {
      this.dustbinData.push(row.id);
      this.deleteToDustBin(row.status);
    },
    handleRestore(index, row) {
      let _this = this;
      this.$confirm("将该文件还原到原处，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          _this.loading = true;
          put("/article/reset/" + row.id).then(resp => {
            if (resp.code == 0) {
              _this.$message({ type: "success", message: resp.message });
            } else {
              _this.$message({ type: "error", message: "还原失败!" });
            }
            _this.loadBlogs(1, this.pageSize);
            _this.loading = false;
          });
        })
        .catch(() => {
          _this.$message({
            type: "info",
            message: "已取消还原"
          });
        });
    },
    deleteToDustBin(state) {
      var _this = this;
      this.$confirm(
        state != 2
          ? "将该文件放入回收站，是否继续?"
          : "永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          _this.loading = true;
          if (state != 2) {
            put("/article/mdel", {
              aids: _this.dustbinData,
              status: 2
            }).then(resp => {
              if (resp.code == 0) {
                _this.$message({ type: "success", message: resp.message });
              } else {
                _this.$message({ type: "error", message: "删除失败!" });
              }
              _this.loading = false;
              _this.dustbinData = [];
              _this.loadBlogs(1, this.pageSize);
            });
          } else {
            let ids = _this.dustbinData.join(",");
            del("/article/" + ids).then(resp => {
              if (resp.code == 0) {
                _this.$message({ type: "success", message: resp.message });
              } else {
                _this.$message({ type: "error", message: "删除失败!" });
              }
              _this.loading = false;
              _this.dustbinData = [];
              _this.loadBlogs(1, this.pageSize);
            });
          }
        })
        .catch(() => {
          _this.$message({
            type: "info",
            message: "已取消删除"
          });
          _this.dustbinData = [];
        });
    }
  }
};
</script>
<style type="text/css">
.blog_table_footer {
  display: flex;
  box-sizing: content-box;
  padding-top: 10px;
  padding-bottom: 0px;
  margin-bottom: 0px;
  justify-content: space-between;
}
</style>
